package com.example.administrator.caigou51.control;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.caigou51.R;


/**
 * Created by cjy on 2016/1/19.
 * 配合viewpager的滑动，实现渐变效果的view
 */
public class ChangeTextIcon extends FrameLayout {
    //上层被选择时显示的图片
    private ImageView top_imageView;
    //下层没被选择时显示的图片
    private ImageView bottom_imageView;
    //上层被选择时显示的文字
    private TextView top_textView;
    //下层没被选择时显示的文字
    private TextView bottom_textView;
    //被选择时显示的布局
    private RelativeLayout top_relativeLayout;
    //没被选择时显示的布局
    private RelativeLayout bottom_relativeLayout;
    //透明度用来显示动画渐变效果
    private float mAlpha = 0f;
    private static final String INSTANCE_STATE = "instance_state";
    private static final String STATE_ALPHA = "state_alpha";

    public ChangeTextIcon(Context context) {
        this(context, null);
    }

    public ChangeTextIcon(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ChangeTextIcon(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initData(attrs, context);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.text_icon, this, true);
        top_imageView = (ImageView) findViewById(R.id.imageView_top);
        bottom_imageView = (ImageView) findViewById(R.id.imageView_bottom);
        top_textView = (TextView) findViewById(R.id.textView_top);
        bottom_textView = (TextView) findViewById(R.id.textView_bottom);
        top_relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout_top);
        bottom_relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout_bottom);
    }

    private void initData(AttributeSet attrs, Context context) {
        //获取自定义的属性
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TextIcon);
        int top = array.getResourceId(R.styleable.TextIcon_top_icon, R.mipmap.ic_launcher);
        int bottom = array.getResourceId(R.styleable.TextIcon_bottom_icon, R.mipmap.ic_launcher);
        float textSize = array.getDimension(R.styleable.TextIcon_text_size, 30);
        //被选择时文字的颜色
        int topTextColor = array.getColor(R.styleable.TextIcon_top_text_color, Color.BLACK);
        //没被选择时文字的颜色
        int bottomTextColor = array.getColor(R.styleable.TextIcon_bottom_text_color, Color.BLACK);
        //显示的文字
        String text = array.getString(R.styleable.TextIcon_text);
        //选择时显示的图片
        top_imageView.setImageResource(top);
        //没被选择时显示的图片
        bottom_imageView.setImageResource(bottom);
        top_textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        top_textView.setTextColor(topTextColor);
        top_textView.setText(text);
        bottom_textView.setTextColor(bottomTextColor);
        bottom_textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        bottom_textView.setText(text);
        array.recycle();
        setIconAlpha(mAlpha);
    }

    /**
     * @param alpha 透明度
     * 根据滑动来实现透明度动画
     * 上层设置为alpha
     * 下层设置为1-alpha
     */
    public void setIconAlpha(float alpha) {
        top_relativeLayout.setAlpha(alpha);
        bottom_relativeLayout.setAlpha(1 - alpha);
        this.mAlpha = alpha;
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        Bundle bundle = new Bundle();
        bundle.putParcelable(INSTANCE_STATE, super.onSaveInstanceState());
        bundle.putFloat(STATE_ALPHA, mAlpha);
        return bundle;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (state instanceof Bundle) {
            Bundle bundle = (Bundle) state;
            mAlpha = bundle.getFloat(STATE_ALPHA);
            super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATE));
        } else {
            super.onRestoreInstanceState(state);
        }

    }
}
